import { Jem } from "../../GameFramework/event/Jem";
import Model from "../../GameFramework/mvc/Model";
import { EPockerStatus, ESuit } from "../ConfigEnum";
import UiPocker from "../View/UiPocker/UiPocker";
import GameEvent from "./GameEvent";

class Pocker{
  public point:number = -1;
  public suit:ESuit = ESuit.HeiTao;
	public ePockerStatus:EPockerStatus = EPockerStatus.CLOSE;
	private _view:UiPocker = null;
  
  constructor(point:number,suit:ESuit,ePockerStatus:EPockerStatus){
    this.point = point;
		this.suit = suit;
		this.ePockerStatus = ePockerStatus;
  }

	public get view():UiPocker{
		return this._view;
	}

	public bind(uiPocker:UiPocker){
		this._view = uiPocker;
	}

	public unBind(){
		this._view = null;
	}
}

class PockerGroup{
	/*****************************************
  * property
  *****************************************/
	private _pockers:Pocker[] = [];

	/*****************************************
   * getter & setter
   *****************************************/
	public get pockers():Pocker[]{ return this._pockers; }

	/*****************************************
   * public API
   *****************************************/
	public addPocker(pocker:Pocker){
		this._pockers.push(pocker);
	}
}


class GameDB extends Model{
	/*****************************************
  * property
  *****************************************/
	private _pockers:Pocker[] = [];
	private _closeAreaPockers:Pocker[] = [];
	private _openAreaPockers:Pocker[] = [];
	private _receiveAreaPockerGroups:PockerGroup[] = [];
	private _playAreaPockerGroups:PockerGroup[] = [];

	public static readonly CONST_RECEIVE_GROUPS:number = 4;
	public static readonly CONST_PLAY_GROUPS:number = 7;
	/*****************************************
   * getter & setter
   *****************************************/
	public get pockers():Pocker[]{ return this._pockers; }
	public get closeAreaPockers():Pocker[]{ return this._closeAreaPockers; }
	public get openAreaPockers():Pocker[]{ return this._openAreaPockers; }
	public get receiveAreaPockerGroups():PockerGroup[]{ return this._receiveAreaPockerGroups; }
	public get playAreaPockerGroups():PockerGroup[]{ return this._playAreaPockerGroups; }

	/*****************************************
	* private API
	*****************************************/
  constructor(){
		super();
	}

  /*****************************************
   * public statis API
   *****************************************/
  public static create():GameDB{
	  let gameDB = new GameDB();
	  return gameDB;
  }

  /*****************************************
   * public API
   *****************************************/
	public init(){
		// 初始化牌局结构
		for (let i = 0; i < GameDB.CONST_RECEIVE_GROUPS; i++) {
			let pockerGroup = new PockerGroup();
			this._receiveAreaPockerGroups.push(pockerGroup);
		}
		for (let i = 0; i < GameDB.CONST_PLAY_GROUPS; i++) {
			let pockerGroup = new PockerGroup();
			this._playAreaPockerGroups.push(pockerGroup);
		}
		//生成Pocker
		for (let point = 1; point <= 13; point++) {
			for (let suit = 0; suit < 4; suit++) {
				let pocker = new Pocker(point,suit,EPockerStatus.CLOSE);
				this._pockers.push(pocker);
			}
		}
		this.fire(GameEvent.ININT_POCKER,{
			"pockers":this._pockers,
		});
	}

	public play(){
		let tmp = this._closeAreaPockers;
		this._closeAreaPockers = this._pockers;
		this._pockers = tmp;
		this.fire(GameEvent.PLAY,this._pockers);
		
		for (let cards = GameDB.CONST_PLAY_GROUPS; cards >=1 ; cards--) {
			for (let i = 0; i < cards; i++) {
				let cardGroupIndex = GameDB.CONST_PLAY_GROUPS - cards + i;
				let cardGroup:PockerGroup = this._playAreaPockerGroups[cardGroupIndex];
				
				let pocker = this._closeAreaPockers[this._closeAreaPockers.length-1];
				this._closeAreaPockers.length = this._closeAreaPockers.length-1;
				//每行的第一张牌是打开状态
				pocker.ePockerStatus = i===0 ? EPockerStatus.OPEN : EPockerStatus.CLOSE;
				cardGroup.addPocker(pocker);
				this.fire(GameEvent.INIT_GROUP_CARD,{
					// 第几组
					'groupIndex':cardGroupIndex,
					// 该组的第几张牌
					'cardIndex':GameDB.CONST_PLAY_GROUPS - cards,
					'pocker':pocker
				})
			}
		}
	}

  

  
}

export{
	Pocker,
	PockerGroup,
	GameDB,
}